package com.visionet.waveletData.web.common.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.visionet.waveletData.web.common.utilities.MessageSourceHelper;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class DateShowUtil {
    private static Log log = LogFactory.getLog(DateShowUtil.class);
	  /**
     * 根据 timestamp 生成各类时间状态串
     * 
     * @param timestamp 距1970 00:00:00 GMT的秒数
     * @return 时间状态串(如：刚刚5分钟前)
     */
    public static String getTimeState(Long timestamp) {

        try {
            long _timestamp = timestamp ;
            if (System.currentTimeMillis() - _timestamp < 1 * 60 * 1000) {
                return MessageSourceHelper.GetMessages("app.common.utils.DateShowUtil.just");
            } else if (System.currentTimeMillis() - _timestamp < 30 * 60 * 1000) {
                return ((System.currentTimeMillis() - _timestamp) / 1000 / 60)
                        + MessageSourceHelper.GetMessages("app.common.utils.DateShowUtil.minute.ago");
			} else {
				Calendar now = Calendar.getInstance();
				Calendar c = Calendar.getInstance();
				c.setTimeInMillis(_timestamp);
				if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)
						&& c.get(Calendar.MONTH) == now.get(Calendar.MONTH)
						&& c.get(Calendar.DATE) == now.get(Calendar.DATE)) {
					SimpleDateFormat sdf = new SimpleDateFormat(
							MessageSourceHelper.GetMessages("app.common.utils.DateShowUtil.today")
									+ " HH:mm");
					return sdf.format(c.getTime());
				}
				if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)
						&& c.get(Calendar.MONTH) == now.get(Calendar.MONTH)
						&& c.get(Calendar.DATE) == now.get(Calendar.DATE) - 1) {
					SimpleDateFormat sdf = new SimpleDateFormat(
							MessageSourceHelper.GetMessages("app.common.utils.DateShowUtil.yesterday")
									+ " HH:mm");
					return sdf.format(c.getTime());
				} else if (c.get(Calendar.YEAR) == now.get(Calendar.YEAR)) {
					SimpleDateFormat sdf = new SimpleDateFormat(
							MessageSourceHelper.GetMessages("app.common.utils.DateShowUtil.today.md")
									+ " HH:mm:ss");
					return sdf.format(c.getTime());
				} else {
					SimpleDateFormat sdf = new SimpleDateFormat(
							MessageSourceHelper.GetMessages("app.common.utils.DateShowUtil.today.ymd")
									+ " HH:mm:ss");
					return sdf.format(c.getTime());
				}
			}
        } catch (Exception e) {
            log.error(e.toString(),e);
            return "";
        }
    }
    
    public static String standardDate(Long time){
    	 Calendar c = Calendar.getInstance();
         c.setTimeInMillis(time);
         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    	return sdf.format(c.getTime());
    }

}
